package com.zhongketech.sunflower.activity.me;

import com.zhongketech.sunflower.R;

import android.R.animator;
import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BottomDialog extends Dialog implements
		android.view.View.OnClickListener {

	private Context mContext;
	private String mTitle = "";
	private String itemNames[];
	private OnClickDialogListener mOnClickDialogListener;
	private Resources rs;
	private int paddingValue;
	private DisplayMetrics dm;
	private LinearLayout rootLayout_s;
	private int itemsNum = 0;

	public String[] getItemNames() {
		return itemNames;
	}

	public void setItemNames(String[] itemNames) {
		this.itemNames = itemNames;
	}
	
	public String getmTitle() {
		return mTitle;
	}

	public void setmTitle(String mTitle) {
		this.mTitle = mTitle;
	}


	public BottomDialog(Context context) {
		super(context);
	}

	public BottomDialog(Context context, int theme) {
		super(context, theme);
		mContext = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initParams();
		setRootLayout();
		setTitleView();
		setButtonView();
		addBtnLitener();

	}
	
	private void initParams() {
		rs = mContext.getResources();
		dm = rs.getDisplayMetrics();
		paddingValue = (int) dm.density;
		if(itemNames !=null){
			itemsNum = itemNames.length;
		}
	}

	private void addBtnLitener() {
		
	}

	private void setButtonView() {
		for(int i=0;i<itemsNum;i++){
			Button btn = new Button(mContext);
			LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
			btn.setTextColor(Color.WHITE);
			btn.setText(itemNames[i]);
			btn.setId(i);
			btn.setOnClickListener(this);
			rootLayout_s.addView(btn);
		}
	}

	private void setTitleView() {
		TextView titleTextView = new TextView(mContext);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		lp.setMargins(0, 10*paddingValue, 0, 10*paddingValue);
		titleTextView.setGravity(Gravity.CENTER_HORIZONTAL);
		titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,rs.getDimension(R.dimen.common_item_text_left_textsize)/dm.scaledDensity);
		titleTextView.setTextColor(Color.WHITE);
		titleTextView.setText(mTitle);
		rootLayout_s.addView(titleTextView);
	}

	private void setRootLayout() {
		// 第一级root 不全屏显示的效果
		RelativeLayout rootLayout = new RelativeLayout(mContext);
		LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		rootLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		setContentView(rootLayout, lp1);
		
		// 第二级 主要是第一级不能设置对其底部属性
		rootLayout_s = new LinearLayout(mContext);
		RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		Resources rs = mContext.getResources();
		rootLayout_s.setBackgroundColor(Color.DKGRAY);
		rootLayout_s.setGravity(Gravity.CENTER_HORIZONTAL);
		rootLayout_s.setOrientation(LinearLayout.VERTICAL);
		rootLayout.addView(rootLayout_s,lp2);
	}

	@Override
	protected void onStart() {
		super.onStart();
		
	}

	public void setOnClickDialogListener(
			OnClickDialogListener onClickDialogListener) {
		if (mOnClickDialogListener == null) {
			mOnClickDialogListener = onClickDialogListener;
		}
	}

	public interface OnClickDialogListener {
		abstract void onClick(int index);
	}

	@Override
	public void onClick(View v) {
		if (mOnClickDialogListener != null) {
			mOnClickDialogListener.onClick(v.getId());
		}
		dismiss();
	}

}
